package classes.form {
	import classes.Arrow;
	import classes.Label;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author dennis
	 */
	public class Select extends FormElement {

		private var valueList : Sprite;
		private var value : Label;

		private var Text : String;

		public function Select(Label : String) : void {
			super();
			this.buttonMode = true;
			this.Text = Label;
			this.formName = Label;
		}

		public function generate( SkinBG : *, Arrow : *,EntryBg : *, data : Array, w : Number = 100, h : Number = 15 ) : void {
			var label : Label = new Label(this.Text);
			label.generate(10, 0x000000);
			this.addChild(label);
			
			var SelectBox : Sprite = new Sprite();
			SelectBox.addEventListener(MouseEvent.MOUSE_DOWN, showList);
			SelectBox.x = this.SPACE_LABEL_FIELD;
			this.addChild(SelectBox);
			
			/*
			 * BG
			 */
			var bg : Sprite = new Sprite();
			if( SkinBG ) {
				bg.addChild( SkinBG ); // Skin as source
			}else {
				bg.graphics.beginFill(0xCDCDCD);
				bg.graphics.drawRect(0, 0, w, h);
				bg.graphics.endFill();
			}
				SelectBox.addChild(bg);
				
			this.value = new Label("-- Please Select --");
			this.value.generate(10, 0x000000);
			this.value.x = 5;
			this.value.y = 2;
			SelectBox.addChild(this.value);
			
			/*
			 * Arrow
			 */
			var dropdownArrow:*;
			if(Arrow) dropdownArrow = Arrow;  // Skin as source
			else dropdownArrow = new Arrow(10, 10, "down"); // Draw Icon
			
			dropdownArrow.x = Math.round(SelectBox.width - (dropdownArrow.width + 5));
			dropdownArrow.y = Math.round(SelectBox.height - (dropdownArrow.height +7));
			SelectBox.addChild(dropdownArrow);
			
			/*
			 * Add Flyout to Dropdown
			 */
			this.valueList = createList(data, true);
			this.valueList.visible = false;
			this.valueList.y = bg.height;
			SelectBox.addChild(this.valueList);
		}

		private function createList(datas : Array, showBG : Boolean) : Sprite {
			var listBox : Sprite = new Sprite();		
			var listBoxBG : Sprite = new Sprite();
			listBoxBG.graphics.beginFill(0xEFEFEF);
			listBoxBG.graphics.drawRect(0, 0, 10, 10);
			listBoxBG.graphics.endFill();
			listBox.addChild(listBoxBG);
			for(var i : int = 0;i < datas.length;i++) {
				var entry : SelectEntry = new SelectEntry(datas[i][0], datas[i][1]);
				entry.y = Math.round(i * entry.height);
				entry.addEventListener(MouseEvent.CLICK, chooseValue);
				entry.addEventListener(MouseEvent.MOUSE_OVER, hoverValue);
				entry.addEventListener(MouseEvent.MOUSE_OUT, hoverValue);
				listBox.addChild(entry);
			}
			listBoxBG.height = listBox.height;
			listBoxBG.width = listBox.width;
			return listBox;
		}

		public function updateList() : void {
		}

		private function showList(e : MouseEvent) : void {
			this.valueList.visible = true;
		}

		private function hideList(e : MouseEvent) : void {
			this.valueList.visible = false;
		}		

		private function chooseValue(e : MouseEvent) : void {
			hideList(e);
			this.formValue = SelectEntry(e.target).getValue();
			this.value.updateText(SelectEntry(e.target).getText());
		}

		private function hoverValue(e : MouseEvent) : void {
			if( e.type == "mouseOver" ) SelectEntry(e.target).rollover();
			else SelectEntry(e.target).rollout();
		}
	}
}
